<?php if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}

class Transact_history_model extends CI_Model {
	public function __construct() {
		parent::__construct();
	}

public function get_status($num,$name,$from,$to){
	/*$where=" WHERE rhv.account_num = '%' ";
	if(($num == "" && $name == "") && ($from != "" || $to != "")) {
		$where = "WHERE DATE_FORMAT(STR_TO_DATE(rhv.invoice_date, '%Y-%m-%d'),'%Y-%m') BETWEEN 
			IFNULL(
				DATE_FORMAT(STR_TO_DATE('".$from."', '%d-%M-%y'),'%Y-%m') ,
				DATE_FORMAT(STR_TO_DATE('".$to."', '%d-%M-%y'),'%Y-%m')) 
			AND 
			IFNULL(
				DATE_FORMAT(STR_TO_DATE('".$to."', '%d-%M-%y'),'%Y-%m') ,
				DATE_FORMAT(STR_TO_DATE('".$from."', '%d-%M-%y'),'%Y-%m'))";
	}else if(($from == '' && $to == '') && ($num != "" || $name != "")){ 
		$where = "WHERE rhv.account_num = '".$num."' OR rhv.acct_name LIKE '".$name."' ";
	}
	else if($from != '' || $to != '' || $num != "" || $name != "" ){ 
		$where = "WHERE (rhv.account_num = '".$num."' OR rhv.acct_name LIKE '".$name."') 
				 AND DATE_FORMAT(STR_TO_DATE(rhv.invoice_date, '%Y-%m-%d'),'%Y-%m') BETWEEN 
				 IFNULL(
				 	DATE_FORMAT(STR_TO_DATE('".$from."', '%d-%M-%y'),'%Y-%m') ,
				 	DATE_FORMAT(STR_TO_DATE('".$to."', '%d-%M-%y'),'%Y-%m'))
				 AND 
				 IFNULL(
				 	DATE_FORMAT(STR_TO_DATE('".$to."', '%d-%M-%y'),'%Y-%m') ,
				 	DATE_FORMAT(STR_TO_DATE('".$from."', '%d-%M-%y'),'%Y-%m'))";
	}
	//var_dump($where);
	$sql = "SELECT * FROM report_history_v rhv ".$where." ORDER BY rhv.invoice_date DESC";

	$query = $this->db->query($sql);
	return $query->result();*/

   $d1 = '';
   $d2 = '';
   if(empty($from)){
    $d1 = $to;
   }else{
    $d1 = $from;
   }

   if(empty($to)){
    $d2 = $from;
   }else{
    $d2 = $to;
   }

 	$sql = "";
    $sql1 = ""; 
    $sql2 = "";
    $sql3 = "";
    $arr = array();

    if(!empty($num)){
      $sql1 = " rhv.account_num LIKE '". $num ."' ";
      array_push($arr,$sql1);
    }

    if(!empty($name)){
      $sql2 = " rhv.acct_name LIKE '". $name ."' ";
      array_push($arr,$sql2);
    }

    if(!empty($from) || !empty($to)){
      $sql3 = " DATE_FORMAT(STR_TO_DATE(rhv.invoice_date, '%Y-%m-%d'),'%Y-%m') BETWEEN 
			IFNULL(
				DATE_FORMAT(STR_TO_DATE('".$d1."', '%d-%M-%y'),'%Y-%m') ,
				DATE_FORMAT(STR_TO_DATE('".$d2."', '%d-%M-%y'),'%Y-%m')) 
			AND 
			IFNULL(
				DATE_FORMAT(STR_TO_DATE('".$d2."', '%d-%M-%y'),'%Y-%m') ,
				DATE_FORMAT(STR_TO_DATE('".$d1."', '%d-%M-%y'),'%Y-%m')) ";
      array_push($arr,$sql3);
    }

    $where = "WHERE ".implode(' AND ',$arr);

    $sql = "SELECT * FROM report_history_v rhv ".$where;

    $q = $this->db->query($sql);
    return $q->result();
    echo $sql;
}


}